﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceCS.Helpers
{
    public class TextHelper
    {
        /// <summary>
        /// 从文本中查找匹配文本数量
        /// </summary>
        /// <param name="searchWord"></param>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public int GetWordCount(string searchWord, string inputString)
        {
            // Null check these variables and determine if they have values.
            if (string.IsNullOrEmpty(searchWord) || string.IsNullOrEmpty(inputString))
            {
                return 0;
            }

            //Convert the string into an array of words  
            var source = inputString.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },
                                           StringSplitOptions.RemoveEmptyEntries);

            // Create the query. Use ToLowerInvariant to match uppercase/lowercase strings.   
            var matchQuery = from word in source
                             where word.ToLowerInvariant() == searchWord.ToLowerInvariant()
                             select word;

            // Count the matches, which executes the query. Return the result.
            return matchQuery.Count();
        }

        /// <summary>
        /// 把输入的英文字符串转换为大写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string ToUpper(string str)
        {
            var charArr = str.ToCharArray();
            var resStr = string.Empty;
            for (var i = 0; i < charArr.Length; i += 1)
            {
                var tmp = (int)charArr[i] - 32;
                charArr[i] = (char)tmp;
                resStr += charArr[i];
            }
            return resStr;
        }
    }
}
